package com.fruit.controller.wap.sms;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fruit.controller.base.BaseController;
import com.fruit.pojo.sms.SmsCodeValidate;
import com.fruit.pojo.user.UserLogin;
import com.fruit.service.sms.SmsCodeManager;
import com.fruit.service.user.UserManager;
import com.fruit.util.ResultInfo;
import com.fruit.util.ResultUtil;
import com.fruit.util.ValidateUtil;

/**
 * wap短信controller
 * @author Administrator
 * @version 2017.12.01 11:40
 */
@Controller
@RequestMapping(value = "/wap/sms/")
public class SmsWapController extends BaseController {

	@Autowired
	private SmsCodeManager smsCodeService;

	@Autowired
	private UserManager userService;

	/**
	 * 发送验证码
	 * @param ul UserLogin
	 * @param type type
	 * @return Map<String, Object>
	 */
	@RequestMapping(value = "/send", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> send(UserLogin ul, String type) {
		Map<String, Object> resMap = new HashMap<String, Object>();
		try {
			ResultInfo ri = ValidateUtil.phone(ul.getPhone());
			if (ri != null) {
				resMap.put("result", ri);
				return resMap;
			}
			if (StringUtils.isEmpty(type)) {
				resMap.put("result", this.initResult(ResultInfo.ERROR, "error", "参数错误!"));
				return resMap;
			}
			UserLogin login = userService.getUserLoginByPhone(ul);
			if (StringUtils.equals(type, "password")) {
				if (login == null) {
					resMap.put("result", this.initResult(ResultInfo.ERROR, "no_exists", "该手机号码尚未注册!"));
					return resMap;
				}
			}
			if (StringUtils.equals(type, "register") || StringUtils.equals(type, "invite")) {
				if (login != null) {
					resMap.put("result", this.initResult(ResultInfo.ERROR, "is_exists", "该手机号码已注册!"));
					return resMap;
				}
			}
			SmsCodeValidate sms = new SmsCodeValidate();
			sms.setPhone(ul.getPhone());
			sms.setTypes(type);
			resMap = smsCodeService.sendSmsCode(sms);
		} catch (Exception e) {
			resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "exception", "发送失败"));
		}
		return resMap;
	}

	/**
	 * 验证码校验
	 * @param ul UserLogin
	 * @param type type
	 * @return Map<String, Object>
	 */
	@RequestMapping(value = "/codeCheck", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> codeCheck(UserLogin ul, String type) {
		Map<String, Object> resMap = new HashMap<String, Object>();
		try {
			ResultInfo ri = ValidateUtil.phone(ul.getPhone());
			if (ri != null) {
				resMap.put("result", ri);
				return resMap;
			}
			if (StringUtils.isEmpty(type)) {
				resMap.put("result", this.initResult(ResultInfo.ERROR, "error", "参数错误!"));
				return resMap;
			}
			String msg = smsCodeService.getValidateCode(ul.getPhone(), ul.getSmsCode(), type);
			if (StringUtils.equals(msg, "expire")) {
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "code_expire", "验证码已失效!"));
				return resMap;
			} else if (!StringUtils.equals(msg, "ok")) {
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "code_error", "验证码有误!"));
				return resMap;
			}
			resMap.put("result", ResultUtil.initResult(ResultInfo.SUCCESS, "SUCCESS", "验证通过!"));
		} catch (Exception e) {
			resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "exception", "验证失败"));
		}
		return resMap;
	}

}
